package Z09Swing.TX02Shijianjianting;


import javax.swing.*;
import java.awt.event.*;
import java.util.Random;


/**
 * @author 李某
 * * @date 2021/10/21
 * *物有本末，事有终始。知其先后者，则近道矣！
 * *荷花开满池塘的前一天还是半塘荷花半塘云。
 */
public class TestListon {
}

// 按钮监听
class TestGUI {
    public static void main(String[] args) {
        JFrame f = new JFrame("菜狗");
        f.setSize(400, 300);
        f.setLocation(580, 200);
        f.setLayout(null);

        final JLabel l = new JLabel();

        ImageIcon i = new ImageIcon("D:\\Documents\\Pictures\\745.png");
        l.setIcon(i);
        l.setBounds(50, 50, i.getIconWidth(), i.getIconHeight());

        JButton b = new JButton("隐藏图片");
        b.setBounds(150, 200, 100, 30);

        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                l.setVisible(false);
            }
        });

        f.add(l);
        f.add(b);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);

    }
}

// 键盘监听
class TestGUI1 {

    public static void main(String[] args) {

        JFrame f = new JFrame("菜狗");
        f.setSize(400, 300);
        f.setLocation(580,200);
        f.setLayout(null);

        final JLabel l = new JLabel();

        ImageIcon i = new ImageIcon("D:\\Documents\\Pictures\\745.png");
        l.setIcon(i);
        l.addKeyListener(new KeyListener() {
            @Override
            // 按键弹起
            public void keyTyped(KeyEvent e) {
                if(e.getKeyCode() == 39){
                    l.setLocation(l.getX() + 10,l.getY());
                }
            }

            @Override
            // 按键按下
            public void keyPressed(KeyEvent e) {

            }

            // 按下弹起的组合动作
            @Override
            public void keyReleased(KeyEvent e) {

            }
        });

        f.add(l);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

// 鼠标监听
class TestGUI3{
    public static void main(String[] args) {
        final JFrame f = new JFrame("caigou");
        f.setSize(800,600);
        f.setLocationRelativeTo(null);
        f.setLayout(null);

        final JLabel l = new JLabel();
        ImageIcon i = new ImageIcon("D:\\Documents\\Pictures\\745.png");
        l.setIcon(i);
        l.setBounds(375,275,i.getIconWidth(),i.getIconHeight());
        f.add(l);

        l.addMouseListener(new MouseListener() {
            // 按下释放组合动作为点击鼠标
            @Override
            public void mouseClicked(MouseEvent e) {

            }

            // 按下鼠标
            @Override
            public void mousePressed(MouseEvent e) {

            }
            // 释放鼠标
            @Override
            public void mouseReleased(MouseEvent e) {

            }

            // 鼠标进入
            @Override
            public void mouseEntered(MouseEvent e) {
                Random r = new Random();
                int x = r.nextInt(f.getWidth() - l.getWidth());
                int y = r.nextInt(f.getHeight() - l.getHeight());

                l.setLocation(x,y);

            }
            // 鼠标退出
            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    }
}